package org.web.webdisplay.sdk.helper;

import org.web.base.domain.InsertGroup;
import org.web.base.domain.UpdateGroup;
import org.web.base.domain.exception.ResultMessageEnum;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.domain.helper.ValidationHelper;
import org.web.webdisplay.sdk.dto.VerifyIdentityDTO;

import java.util.Calendar;
import java.util.Date;

public class VerifyIdentityHelper {

    public static void judgeGenerateVerifyIdentityCode(VerifyIdentityDTO verifyIdentityDTO) {
        ValidationHelper.validate(VerifyIdentityDTO.class, verifyIdentityDTO, InsertGroup.class);

        // 1、超期时间不能小于当前时间，且不能长于7天。
        Date currentDate = new Date();
        Date expiredEndDate = verifyIdentityDTO.getExpiredEndDate();
        if (expiredEndDate == null) {
            // 默认设置5分钟过期。
            expiredEndDate = new Date(System.currentTimeMillis() + 5 * 60 * 1000);
            verifyIdentityDTO.setExpiredEndDate(expiredEndDate);
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_YEAR, 7);
        Date sevenDaysLater = calendar.getTime();
        if (expiredEndDate.before(currentDate)) {
            throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_INVALID, "expiredEndDate不能小于当前时间");
        }
        if (expiredEndDate.after(sevenDaysLater)) {
            throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_INVALID, "expiredEndDate不能长于7天");
        }

        // 2、根据noticeWay（1:邮箱;2:短信）， 校验sendMark合法性。判断是否为手机号码、邮箱地址。
        String sendMark = verifyIdentityDTO.getSendMark();
        Short noticeWay = verifyIdentityDTO.getNoticeWay();
        if (noticeWay == 0) {
            /* do nothing */
        } else if (noticeWay == 1) { // 邮箱
            if (!isValidEmail(sendMark)) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_INVALID, "sendMark不是有效的邮箱地址");
            }
        } else if (noticeWay == 2) { // 短信
            if (!isValidPhoneNumber(sendMark)) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_INVALID, "sendMark不是有效的手机号码");
            }
        } else {
            throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_INVALID, "noticeWay参数无效");
        }
    }

    public static void judgeVerifyIdentityCode(VerifyIdentityDTO verifyIdentityDTO) {
        Integer allowMaxCheckTimes = verifyIdentityDTO.getAllowMaxCheckTimes();
        if (allowMaxCheckTimes == null) {
            allowMaxCheckTimes = 3;
            verifyIdentityDTO.setAllowMaxCheckTimes(allowMaxCheckTimes);
        }
        ValidationHelper.validate(VerifyIdentityDTO.class, verifyIdentityDTO, UpdateGroup.class);
    }


    public static boolean isValidEmail(String email) {
        // 使用正则表达式校验邮箱地址
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        return email != null && email.matches(emailRegex);
    }

    public static boolean isValidPhoneNumber(String phoneNumber) {
        // 使用正则表达式校验手机号码，假设手机号码为11位数字
        String phoneRegex = "^1[3-9]\\d{9}$";
        return phoneNumber != null && phoneNumber.matches(phoneRegex);
    }

}
